<?php


	//Script to execute various portions of Bluebird Reports from shell




	function printHelp() {
		print("\n");
		print("Usage: php -f brsh.php [args...]\n");
		print("\n");
		print("Arguments:\n");
		print("\tusername=<username>\t\t\tThe username under which to execute the requested action\n");
		print("\tpassword=<password>\t\t\tThe corresponding password\n");
		print("\ttemplate=<template file>\t\tThe filename of the template to execute. Note: omit path information.\n");
		print("\tvariables=\"var1=val1; var2=val2; ...\"\tOptional. Allows you to override template variables\n");
		print("\n");
		print("\n");
		print("Example:\n");
		print("\tphp -f brsh.php\n");
		print("\t\t\tusername=johndoe\n");
		print("\t\t\tpassword=1234\n");
		print("\t\t\ttemplate=My_First_Template.xml\n");
		print("\t\t\tvariables=\"My first variable=1234; My second varible=5678;\"\n");
	}



	$validArguments = array("username", "password", "template", "variables");
	$hasError = false;



	//Gather arguments in a map
	$cliArgs = array();
	for ($i=1; $i<$argc; $i++) {
		$eqPos = strpos($argv[$i], "=");
		if (!$eqPos) {
			print("Error: Arguments need to be on the form A=B\n");
			$hasError = true;
			break;
		} else {
			$pName  = substr($argv[$i], 0, $eqPos);
			$pValue = substr($argv[$i], $eqPos+1, strlen($argv[$i])-($eqPos+1));
			$cliArgs[$pName] = $pValue;
		}
	}

	if ($hasError || (sizeof($cliArgs) == 0) ) {
		printHelp();
		exit(1);
	}



	//Check for non-supported arguments
	foreach ($cliArgs as $name=>$value) {
		if (!in_array($name, $validArguments)) {
			print("Error: Unknown argument \"".$name."\"\n");
			$hasError = true;
			break;
		}
	}

	if ($hasError) {
		printHelp();
		exit(1);
	}



	//Validate user
	if (!isset($cliArgs["username"]) || !isset($cliArgs["password"])) {
		print("Error: You have to supply a username and a password\n");
		printHelp();
		exit(1);
	}


	require_once("appconf.php");

	$username = $cliArgs["username"];
	$password = $cliArgs["password"];

	if (!$userValidator->validate($username, $password)) {
		print("Error: Wrong username and/or password\n");
		printHelp();
		exit(1);
	}




	//Run template
	if (!isset($cliArgs["template"])) {
		print("Error: You have to supply a template filename\n");
		printHelp();
		exit(1);
	}

	require_once("lib/utility.php");
	require_once("query_includes.php");
	require_once("lib/class.Template.php");

	$templateObj = new Template($username, $cliArgs["template"]);
	if (!$templateObj->isReady()) {
		print("Error: '".$cliArgs["template"]."' is not a valid template filename\n");
		printHelp();
		exit(1);
	}


	$overrideMap = array();
	if (isset($cliArgs["variables"])) {
		$vars = explode(";", $cliArgs["variables"]);
		for ($i=0; $i<sizeof($vars); $i++) {
			if (strlen($vars[$i]) > 0) {
				$eqPos = strpos($vars[$i], "=");
				if (!$eqPos) {
					print("Error: Overriding variables need to be on the form A=B;\n");
					$hasError = true;
					break;
				} else {
					$vName  = substr($vars[$i], 0, $eqPos);
					$vValue = substr($vars[$i], $eqPos+1, strlen($vars[$i])-($eqPos+1));
					$overrideMap[$vName] = $vValue;
				}
			}
		}
	}

	if ($hasError) {
		printHelp();
		exit(1);
	}


	$outputFile = $templateObj->run($overrideMap);

	print($outputFile . "\n");
	exit(0);

?>